<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @cancel="handleCancel">
    <template #footer>
      <a-button key="1" @click="handleCancel">关闭</a-button>
      <a-button
        :hidden="disableSubmit"
        key="3"
        type="primary"
        :loading="loading"
        @click="onSure"
        v-if="!disabled">确定</a-button>
    </template>

    <teamChoose ref="realForm" :formType="formType" :disabled="disabled" :publishId="publishId" :intensiveType="intensiveType"/>

  </j-modal>
</template>

<script>
import teamChoose from './teamChoose'

export default {
  name: 'TeamModal',
  components: {
    teamChoose
  },
  props: {
    // 区分是通知公告or意见征集
    formType: {
      type: String,
      default: 'notice'
    },
    // 当前选中的
    selectEmpList: {
      type: Array,
      default: () => []
    },
    // 只查看，不能操作
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    publishId: {
      type: String,
      default: ''
    },
    // 选择的密级
    intensiveType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      title: '',
      width: 1400,
      visible: false,
      disableSubmit: false,
      loading: false
    }
  },
  methods: {
    // 初始化回显工作
    init (progress, proTxt) {
      this.visible = true

      this.width = this.disabled ? 500 : 1400

      this.title = proTxt ? '签发' : '选择'

      this.$nextTick(() => {
        this.$refs.realForm.md = this.disabled ? 24 : 9

        this.$refs.realForm.progress = progress || '0' // 流程编号

        // this.$refs.realForm.collKey = '1'
        this.$refs.realForm.realName = ''
        // 判断当前选中是否有值
        if (this.selectEmpList.length === 0) {
          this.$refs.realForm.selectionRows = []
          this.$refs.realForm.selectedRowKeys = []
          this.$refs.realForm.firstSelectData = []
        } else {
          let list = JSON.parse(JSON.stringify(this.selectEmpList))
          this.$refs.realForm.selectedRowKeys = list.map(r => {
            return r.id
          })
          this.$refs.realForm.selectionRows = list
          this.$refs.realForm.firstSelectData = list
        }

        this.$refs.realForm.callback()
        // this.$refs.realForm.getmidList()
      })
    },
    // 确定选择
    async onSure() {
      let selectList = this.$refs.realForm.selectionRows

      if (selectList.length > 0) {
        selectList = selectList.map(rr => {
          return {
            id: rr.id,
            departId: rr.departIds,
            empId: rr.empId,
            username: rr.username,
            realname: rr.realname,
            departName: rr.departName
          }
        })
      }
      this.$emit('sure', selectList);
      this.loading = false
      this.visible = false
    },
    handleCancel () {
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>
